package offer;

/**
 * @Author Elephas
 * @Date 2022/3/18
 **/
public class Bank {
    private long[] balance;
    private int balanceNum;
    public Bank(long[] balance) {
        this.balance = balance;
        this.balanceNum = balance.length;
    }

    private boolean isValidAccount(int account){
        return account >= 1 && account <= this.balanceNum;
    }
    public boolean transfer(int account1, int account2, long money) {
        if(!isValidAccount(account1)){
            return false;
        }else if(!isValidAccount(account2)){
            return false;
        }else if(this.balance[account1 - 1] < money){
            return false;
        }else{
            this.balance[account1 - 1] -= money;
            this.balance[account2 - 1] += money;
            return true;
        }
    }

    public boolean deposit(int account, long money) {
        if(!isValidAccount(account)){
            return false;
        }else if(money <= 0){
            return false;
        }else{
            this.balance[account - 1] += money;
            return true;
        }
    }

    public boolean withdraw(int account, long money) {
        if(!isValidAccount(account)){
            return false;
        }else if(money <= 0){
            return false;
        }else if(this.balance[account - 1] < money){
            return false;
        }else{
            this.balance[account - 1] -= money;
            return true;
        }
    }

}

/**
 * Your Bank object will be instantiated and called as such:
 * Bank obj = new Bank(balance);
 * boolean param_1 = obj.transfer(account1,account2,money);
 * boolean param_2 = obj.deposit(account,money);
 * boolean param_3 = obj.withdraw(account,money);
 */